$(function() {
    var str=localStorage.getItem("users");
    var usersLocal=JSON.parse(str);
    console.log(str);
    console.log(usersLocal);
    var code = "";

    function createCode() {
        newCode = "";
        var codeLength = 4;
        var oldCoed = $("#codeto").get(0);
        var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        for (var i = 0; i < codeLength; i++) {
            var index = parseInt(Math.random() * 36)
            newCode += random[index];
        }
        oldCoed.value = newCode;
        code = oldCoed.value;
    }
    $("#codeto").click(function() {
        createCode();
    });
    var usernames = [];
    var passwords = [];
    for (let i = 0; i < usersLocal.length; i++) {
        usernames.push(usersLocal[i].username);
        passwords.push(usersLocal[i].password);
    }

    var username = "";
    var password = "";
    var phone = "";
    layui.use('form', function() {
        var form = layui.form;
        form.verify({
            username: function(value) {
                if (usernames.indexOf(value) >= 0) {
                    return "用户名已存在";
                }
            },
            rePassword: function(value) {
                if ($("input[name=password]").val() != value) {
                    return "两次密码输入不一致";
                }
            },
            code: function(value) {
                if (value != code) {
                    return '验证码输入不正确';
                }
            }
        })

        // console.log($("input[name=username]").val(), $("input[name=password]").val(), $("input[name=phone]").val());

        //监听提交
        form.on('submit(formDemo)', function(data) {
            layer.msg(JSON.stringify(data.field));
            console.log(data.field);
            console.log($("input[name=username]").val(), $("input[name=password]").val(), $("input[name=phone]").val());
            var user = {
                "id": usersLocal.length + 1,
                "username": $("input[name=username]").val(),
                "password": $("input[name=password]").val(),
                "phone": $("input[name=phone]").val()
            };
            usersLocal[usersLocal.length] = user;
            console.log(usersLocal);
            localStorage.setItem("users", JSON.stringify(usersLocal));
            return true;
        });
    });

    $(".registerBtn").click(function() {
        // console.log($("input[name=username]").val(), $("input[name=password]").val(), $("input[name=phone]").val());
        // var user = {
        //     "id": users.length + 1,
        //     "username": $("input[name=username]").val(),
        //     "password": $("input[name=password]").val(),
        //     "phone": $("input[name=phone]").val()
        // };
        // users[users.length] = user;
        // localStorage.setItem("users", JSON.stringify(users));
    });

});